package com.yenlien.traceyun.breed.controller;

import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.breed.entity.BreedInputs;
import com.yenlien.traceyun.breed.entity.BreedInputsLog;
import com.yenlien.traceyun.breed.service.BreedInputsService;
import com.yenlien.traceyun.business.interceptor.UserAuthUtil;
import com.yenlien.traceyun.webase.controller.BaseController;
import com.yenlien.traceyun.webase.utils.CheckParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by LiuSha on 2020/9/22.
 */
@Api(tags = "养殖投入品管理")
@RestController
@RequestMapping("/breedInputs")
public class BreedInputsController extends BaseController {

    @Autowired
    private BreedInputsService breedInputsService;

    @RequestMapping(value = "/findByList", method = RequestMethod.POST)
    @ApiOperation(value = "列表", notes = "参数示例：{\"page\":\"1\",\"query\":\"饲料\"}")
    public Object findByList(@RequestBody BreedInputs breedInputs) {
        breedInputs.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        return buildPage(breedInputsService.findByList(breedInputs));
    }

    @RequestMapping(value = "/findByLogList", method = RequestMethod.POST)
    @ApiOperation(value = "使用列表", notes = "参数示例：{\"page\":\"1\",\"userName\":\"张三\"}")
    public Object findByLogList(@RequestBody BreedInputsLog breedInputsLog) {
        CheckParams.checkBean(breedInputsLog, new String[]{"inputsId"});
        return buildPage(breedInputsService.findByLogList(breedInputsLog));
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiOperation(value = "新增或修改", notes = "新增或修改时将整个对象传过来，修改时将要修改的整个对象获取到，把需要修改的字段修改就行")
    public Object save(BreedInputs breedInputs, HttpServletRequest request) throws Exception {
        List<MultipartFile> files = new ArrayList<>();
        if (!StringUtils.isEmpty(((MultipartHttpServletRequest) request).getFile("files"))) {
            files = ((MultipartHttpServletRequest) request).getFiles("files");
        }
        breedInputs.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        breedInputsService.save(breedInputs, files);
        return success("success");
    }

    @RequestMapping(value = "/save2", method = RequestMethod.POST)
    @ApiOperation(value = "新增或修改", notes = "新增或修改时将整个对象传过来，修改时将要修改的整个对象获取到，把需要修改的字段修改就行")
    public Object save2(@RequestBody BreedInputs breedInputs) throws Exception {
        breedInputs.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        breedInputsService.save2(breedInputs);
        return success("success");
    }

    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "查看", notes = "")
    public Object findById(@PathVariable String id) {
        return success(breedInputsService.findById(id));
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "删除", notes = "")
    public Object delete(@PathVariable String id) {
        breedInputsService.delete(id);
        return success("success");
    }


    @RequestMapping(value = "/findByAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取所以投入品品名", notes = "")
    public Object findByAll() {
        return success(breedInputsService.findByAll(UserAuthUtil.getLoginUser().getCompanyId()));
    }

    @RequestMapping(value = "/findByType", method = RequestMethod.POST)
    public Object findByType(@RequestBody JSONObject params) {
        return success(breedInputsService.findByType(params.getString("type"), UserAuthUtil.getLoginUser().getCompanyId()));
    }

    @RequestMapping(value = "/findInputsByType", method = RequestMethod.POST)
    public Object findInputsByType(@RequestBody JSONObject params) {
        return success(breedInputsService.findInputsByType(params.getString("type"), UserAuthUtil.getLoginUser().getCompanyId()));
    }

    @RequestMapping(value = "/findByInputsName", method = RequestMethod.POST)
    @ApiOperation(value = "根据品名获取投入品批次", notes = "")
    public Object findByInputsName(@RequestBody JSONObject params) {
        return success(breedInputsService.findByInputsName(params.getString("inputsName"), UserAuthUtil.getLoginUser().getCompanyId()));
    }
}
